/**
 * 比赛模型
 *
 * 任务1
 * 获去正在进行的比赛的数量
 *
 *
 * 任务2
 * 获取结束的比赛数量
 *
 *
 * 任务3
 * 获取正在进行的比赛项目
 *
 *
 * 任务4
 * 获取结束的比赛项目
 *
 * 
 * 任务5
 * 根据guid获取比赛信息
 */

module.exports = class extends think.Model
{

    get pk(){
        return 'PK_CompetitionGuid';
    }

    get relation()
    {
        return {
            competitionsteps: {
                type: think.Model.HAS_MANY,
                key: 'PK_CompetitionGuid',
                fKey: 'FK_CompetitionGuid'
            }
        }
    }

    async getNowTotal()
    {
        return await this.where({status: '进行中','IsDelete':'未删除'}).count();
    }

    async getFinishedTotal()
    {
        return await this.where({status: '已结束','IsDelete':'未删除'}).count();
    }

    async getNowItems()
    {
        let status = {"status": '进行中','IsDelete':'未删除'};
        let fields = 'PK_CompetitionGuid as guid,Title,CoverUrl,GameStartTime,GameEndTime,Status';
        return this._getItems(status, fields);

    }

    async getFinishedItems()
    {
        let status = {"status": '已结束','IsDelete':'未删除'};
        let fields = 'PK_CompetitionGuid as guid,Title,CoverUrl,GameStartTime,GameEndTime,Status';
        return this._getItems(status, fields);
    }

    /**
     * 根据条件和字段获取数据
     * @param status obj 条件 {status: '已结束'}
     * @param field  string 查询字段  "PK_CompetitionGuid as guid,Title,CoverUrl,GameStartTime,GameEndTime,Status"
     * @returns {Promise.<*>}
     */
    async _getItems(status = {}, field = {})
    {
        return await this.where(status).field(field).select();
    }

    /**
     * 根据guid获取比赛信息
     * @param guid
     * @returns {Promise.<void>}
     */
    async getCPByGuid(guid){
        return await this.where({PK_CompetitionGuid:guid}).find();
    }





};